package com.sunday.volunteer.ui.mine;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.GroupType;
import com.sunday.volunteer.entity.Member;
import com.sunday.volunteer.http.HttpClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/1/12.
 */

public class AddGroupActivity extends BaseActivity {

    @Bind(R.id.group_type)
    TextView grouptype;
    @Bind(R.id.group_name)
    TextView groupName;
    @Bind(R.id.title_text)
    TextView titleText;
    private long groupId = -1;
    private List<GroupType> list = new ArrayList<>();
    private List<Member> memberList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_group);
        ButterKnife.bind(this);
        titleText.setText("添加志愿团队");
        getGroup();
    }

    @OnClick(R.id.btn_submit)
    void addGroup() {
        if (groupId == -1) {
            ToastUtils.showToast(mContext, "请选择组织");
            return;
        }
        Call<ResultDO> call = HttpClient.getHttpAdapter().join(BaseApp.getInstance().getMemberId(), groupId);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext , "提交成功，请等待审核");
                    finish();
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }

            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {

            }
        });
    }

    private void getGroup() {
        Call<ResultDO<List<GroupType>>> call = HttpClient.getHttpAdapter().select("groupType");
        call.enqueue(new Callback<ResultDO<List<GroupType>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<GroupType>>> call, Response<ResultDO<List<GroupType>>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    list.addAll(response.body().getResult());
                    if (list.size() > 0) {
                        grouptype.setText(list.get(0).getName());
                        getGroups(list.get(0).getName());
                    } else {
                        grouptype.setText("");
                    }
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<GroupType>>> call, Throwable t) {

            }
        });

    }

    @OnClick(R.id.group_type)
    void groupType() {
        showListDialog1();
    }

    @OnClick(R.id.group_name)
    void groupName() {
        showListDialog2();
    }

    private void showListDialog1() {
        final String[] strings = new String[list.size()];
        if(list.size() == 0){
            ToastUtils.showToast(mContext, "暂时还没有组织类型");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            strings[i] = list.get(i).getName();
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setItems(strings, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                grouptype.setText(strings[which]);
                getGroups(strings[which]);
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void getGroups(String type) {
        Call<ResultDO<List<Member>>> call = HttpClient.getHttpAdapter().getGruops(null, 1, 100, type, null);
        call.enqueue(new Callback<ResultDO<List<Member>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Member>>> call, Response<ResultDO<List<Member>>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    memberList.clear();
                    memberList.addAll(response.body().getResult());
                    if (memberList.size() > 0) {
                        groupId = memberList.get(0).getId();
                        groupName.setText(memberList.get(0).getName());
                    } else {
                        groupId = -1;
                        ToastUtils.showToast(mContext, "该类型下暂无组织，请重新选择");
                        groupName.setText(null);
                    }
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Member>>> call, Throwable t) {
            }
        });
    }

    private void showListDialog2() {
        final String[] strings = new String[memberList.size()];
        if(memberList.size() == 0){
            ToastUtils.showToast(mContext, "该类型下暂无组织，请重新选择");
            return;
        }
        for (int i = 0; i < memberList.size(); i++) {
            strings[i] = memberList.get(i).getName();
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setItems(strings, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                groupName.setText(strings[which]);
                groupId = memberList.get(which).getId();
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }


}
